package com.xmad.ksadutils.AdSDK.AdMold.Kwai;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsScene;
import com.kwad.sdk.api.KsSplashScreenAd;
import com.xmad.ksadutils.AdSDK.AdBase.AdBase;

import java.util.HashMap;
import java.util.Map;

public class KsLoadSplashScreenAd2 extends AdBase {
    private volatile static KsLoadSplashScreenAd2 mInstance;
    KsSplashScreenAd ad;
    static Map<Long, AdComplete> mMap= new HashMap<Long, AdComplete>();
    /**
     * 单例模式
     *
     * @return
     */
    public static KsLoadSplashScreenAd2 getInstance() {
        if (mInstance == null) {
            synchronized (KsLoadSplashScreenAd2.class) {
                if (mInstance == null) {
                    mInstance = new KsLoadSplashScreenAd2();
                }
            }
        }
        return mInstance;
    }

    @Override
    protected void load() {
        if (getKSid()==null||getKSid().length==0){
            Log.e(TAG,"先设置梯度ID");
            return;
        }
        //新建
        NewMap();
        //获取广告对象List列表
        GetAdList();
    }


    //新建
    public void NewMap(){
        Log.e(TAG,mMap.toString());
        if (mMap.size()==0){
            for (Long Id:getKSid()){
                mMap.put(Id,null);
            }
        }
    }

    //获取广告对象List列表
    public void GetAdList(){
        for(Long key:mMap.keySet()){
            if (mMap.get(key)==null||mMap.get(key).getAd()==null){
                Log.e(TAG,key+"开始加载");
                getAds(key);
            }
        }
    }

    private void   getAds(Long id){

        KsScene scene = new KsScene.Builder(id)
                //是否需要开屏小窗展示，默认为false, 设置false后将不会回调 onShowMiniWindow
                .needShowMiniWindow(true)
                .build(); // 此为测试posId，请联系快手平台申请正式posId
        KsAdSDK.getLoadManager().loadSplashScreenAd(scene, new KsLoadManager.SplashScreenAdListener() {
            @Override
            public void onError(int code, String msg) {
                Log.e(TAG,code+msg);
                if (listener!=null){
                    listener.onJump(404);
                    return;
                }

            }
            @Override
            public void onRequestResult(int adNumber) {

            }
            @Override
            public void onSplashScreenAdLoad(@NonNull KsSplashScreenAd splashScreenAd) {
                // addView(splashScreenAd,frameLayout);
                ad = splashScreenAd;
                mMap.put(id,new AdComplete(true,ad));
                if (!isReveal()){
                    return;
                }
                View view = null;
                for(Long key:mMap.keySet()) {
                    if (mMap.get(key) != null && mMap.get(key).getAd() != null) {
                        Log.e(TAG,key+"加载成功");
                        view = mMap.get(key).getAd().getView(activity, new KsSplashScreenAd.SplashScreenAdInteractionListener() {
                            @Override
                            public void onAdClicked() {
                                if (listener!=null){
                                    listener.onJump(200);
                                }
                            }

                            @Override
                            public void onAdShowError(int code, String extra) {
                                ad = null;
                            }

                            @Override
                            public void onAdShowEnd() {
                                if (listener!=null){
                                    listener.onJump(200);
                                    Log.e(TAG,"开屏————————跳转:");
                                }

                            }

                            @Override
                            public void onAdShowStart() {
                                Log.e(TAG,key+"显示");
                            }

                            @Override
                            public void onSkippedAd() {
                                if (listener!=null){
                                    listener.onJump(400);
                                }
                            }

                            @Override
                            public void onDownloadTipsDialogShow() {

                            }

                            @Override
                            public void onDownloadTipsDialogDismiss() {

                            }

                            @Override
                            public void onDownloadTipsDialogCancel() {

                            }
                        });
                        break;
                    }
                }

                if (!activity.isFinishing()) {
                    view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                    frameLayout.addView(view);
                }
            }
        });
    }


    @Override
    protected void start() {

    }

    public class AdComplete{
        private boolean Complete;
        private KsSplashScreenAd ad;

        public AdComplete(boolean complete, KsSplashScreenAd ad) {
            Complete = complete;
            this.ad = ad;
        }

        public boolean isComplete() {
            return Complete;
        }

        public void setComplete(boolean complete) {
            Complete = complete;
        }

        public KsSplashScreenAd getAd() {
            return ad;
        }

        public void setAd(KsSplashScreenAd ad) {
            this.ad = ad;
        }
    }
}
